package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.EditProfileActivityContract
import com.ewzj.bigdata.mvp.model.bean.UserBean
import com.ewzj.bigdata.mvp.model.reqbean.ReqSelfInfoData
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.github.rubensousa.bottomsheetbuilder.BottomSheetBuilder
import com.github.rubensousa.bottomsheetbuilder.adapter.BottomSheetItemClickListener
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.UiUtils
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import top.zibin.luban.Luban
import java.io.File
import javax.inject.Inject


/**
 * author:zhangjing
 * 作用:
 * return:
 */
class EditProfileActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: EditProfileActivityContract.View, model: EditProfileActivityContract.Model) : BasePresenter<EditProfileActivityContract.Model, EditProfileActivityContract.View>(model, view) {
        val context =mRootView as AppCompatActivity
        fun updateUserInfo(userBean: UserBean){
            var reqslef=ReqSelfInfoData()
            reqslef.nickname=userBean.nickname
            reqslef.sign=userBean.sign
            reqslef.sex=userBean.sex.toInt()
            mModel.updateUserinfo(reqslef).compose(RxUtils.applySchedulers(mRootView))
                    .subscribe(object :ErrorHandleSubscriber<BaseData<Any>>(mErrorHandler){
                        override fun onNext(t: BaseData<Any>?) {
//                            UiUtils.SnackbarText("修改成功")
                        }

                    })
        }
    fun uploadPic(filePath:String){
      Flowable.just(File(filePath))
        .observeOn(Schedulers.io())
        .map{
            t ->  Luban.with(context).load(t).get()
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({
            t ->run{
            mModel.upLoadPic(t).compose(RxUtils.applySchedulers(mRootView))
                    .subscribe({t: BaseData<List<String>>? ->  mRootView.updateUserhead(t!!.data!![0])},
                            {UiUtils.SnackbarText("上传失败")})
        }
        })

    }

    fun updateSex(userBean: UserBean){
        var dialog=BottomSheetBuilder(context, R.style.AppTheme_BottomSheetDialog)
                .setMode(BottomSheetBuilder.MODE_LIST)
                .setMenu(R.menu.menu_bottom_sheet)
                .setItemClickListener {item ->
                    var sexint="1"
                    if (item.title.toString().equals("男")){
                        sexint="1"
                    }else{
                        sexint="2"
                    }
                    userBean.sex=sexint
                    updateUserInfo(userBean)
                    mRootView.changeSex(item.title.toString())}
                .createDialog()
        dialog.show()
    }
}